<?php
/**
 * base.php LUOCMS框架入口
 * @Author: Administrator
 * @Date:   2018-11-28 14:01:28
 * @Last Modified by:   Administrator
 * @Last Modified time: 2018-11-28 17:36:02
 */

define('IN_LUOCMS', true);

// 框架路径
define('LC_PATH', dirname(__FILE__).DIRECTORY_SEPARATOR);

if(!defined('LUOCMS_PATH')) define('LUOCMS_PATH', LC_PATH . '..' . DIRECTORY_SEPARATOR);

// 配置目录
define('CONFIG_PATH', LC_PATH . '..' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR);

// 缓存文件路径
define('CACHE_PATH', LC_PATH . '..' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR);

//主机协议
define('SITE_PROTOCOL', isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://');

//当前访问的主机名
define('SITE_URL', (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''));

//来源
define('HTTP_REFERER', isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '');

// 系统开始时间
define('SYS_START_TIME', microtime());

// 加载公共函数库 
base::load_sys_func('global');
base::load_config('system','errorlog') ? set_error_handler('my_error_handler') : error_reporting(E_ERROR | E_WARNING | E_PARSE);

//设置本地时差
function_exists('date_default_timezone_set') && date_default_timezone_set(base::load_config('system','timezone'));

define('CHARSET' ,base::load_config('system','charset'));
//输出页面字符集
header('Content-type: text/html; charset='.CHARSET);

define('SYS_TIME', time());
//定义网站根路径
define('WEB_PATH',base::load_config('system','web_path'));
//js 路径
define('JS_PATH',base::load_config('system','js_path'));
//css 路径
define('CSS_PATH',base::load_config('system','css_path'));
//img 路径
define('IMG_PATH',base::load_config('system','img_path'));
//动态程序路径
define('APP_PATH',base::load_config('system','app_path'));

if(base::load_config('system','gzip') && function_exists('ob_gzhandler')) {
	ob_start('ob_gzhandler');
} else {
	ob_start();
}


class base {
	
	/**
	 * [初始化程序]
	 * @return [type] [description]
	 */
	public static function init(){
		return self::load_sys_class('application');
	}

	/**
	 * [加载系统类方法]
	 * @param  string  $classname  [类名]
	 * @param  string  $path       [扩展地址]
	 * @param  integer $initialize [是否初始化]
	 * @return [type]              [description]
	 */
	public static function load_sys_class($classname, $path = '', $initialize = 1){
		return self::_load_class($classname, $path, $initialize);
	}

	/**
	 * [加载类文件函数]
	 * @param  string  $classname  [类名]
	 * @param  string  $path       [扩展地址]
	 * @param  integer $initialize [是否初始化]
	 * @return [type]              [description]
	 */
	public static function _load_class($classname, $path = '', $initialize = 1){
		static $classes = array();
		if(empty($path)) $path = 'library' . DIRECTORY_SEPARATOR . 'classes';
		$key = md5($path.$classname);
		if(isset($classes[$key])){
			if (!empty($classes[$key])) {
				return $classes[$key];
			} else {
				return true;
			}
		}
		if(file_exists(LC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php')){
			include LC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php';
			$name = $classname;
			if($my_path = self::my_path(LC_PATH.$path.DIRECTORY_SEPARATOR.$classname.'.class.php')) {
				include $my_path;
				$name = 'MY_'.$classname;
			}
			if($initialize) {
				$classes[$key] = new $name;
			}else {
				$classes[$key] = true;
			}
			return $classes[$key];
		}else{
			return false;
		}
	}

	/**
	 * [自己扩展的文件]
	 * @param  string $filepath [路径]
	 * @return [type]           [description]
	 */
	public static function my_path($filepath){
		$path = pathinfo($filepath);
		if (file_exists($path['dirname'].DIRECTORY_SEPARATOR.'MY_'.$path['basename'])) {
			return $path['dirname'].DIRECTORY_SEPARATOR.'MY_'.$path['basename'];
		} else {
			return false;
		}
	}

	/**
	 * [加载系统函数库]
	 * @param  string $func [函数库名]
	 * @return [type]       [description]
	 */
	public static function load_sys_func($func){
		return self::_load_func($func);
	}

	/**
	 * [加载函数库文件]
	 * @param  string $func [函数名]
	 * @param  string $path [扩展地址]
	 * @return [type]       [description]
	 */
	public static function _load_func($func, $path = ''){
		static $funcs = array();
		if (empty($path)) $path = 'library'.DIRECTORY_SEPARATOR.'functions';
		$path .= DIRECTORY_SEPARATOR.$func.'.func.php';
		$key = md5($path);
		if (isset($funcs[$key])) return true;
		if (file_exists(LC_PATH.$path)) {
			include LC_PATH.$path;
		} else {
			$funcs[$key] = false;
			return false;
		}
		$funcs[$key] = true;
		return true;
	}

	/**
	 * [加载配置文件]
	 * @param  [type]  $file    [配置文件]
	 * @param  string  $key     [要获取的配置荐]
	 * @param  string  $default [默认配置。当获取配置项目失败时该值发生作用。]
	 * @param  boolean $reload  [强制重新加载。]
	 * @return [type]           [description]
	 */
	public static function load_config($file, $key = '', $default = '', $reload = false){
		static $configs = array();
		if (!$reload && isset($configs[$file])) {
			if (empty($key)) {
				return $configs[$file];
			} elseif (isset($configs[$file][$key])) {
				return $configs[$file][$key];
			} else {
				return $default;
			}
		}
		$path = CONFIG_PATH.DIRECTORY_SEPARATOR.$file.'.php';
		if (file_exists($path)) {
			$configs[$file] = include $path;
		}
		if (empty($key)) {
			return $configs[$file];
		} elseif (isset($configs[$file][$key])) {
			return $configs[$file][$key];
		} else {
			return $default;
		}
	}

	/**
	 * [加载应用类方法]
	 * @param  [type]  $classname  [类名]
	 * @param  string  $m          [模块]
	 * @param  integer $initialize [是否初始化]
	 * @return [type]              [description]
	 */
	public static function load_app_class($classname, $m = '', $initialize = 1) {
		$m = empty($m) && defined('ROUTE_M') ? ROUTE_M : $m;
		if (empty($m)) return false;
		return self::_load_class($classname, 'modules'.DIRECTORY_SEPARATOR.$m.DIRECTORY_SEPARATOR.'classes', $initialize);
	}

	/**
	 * [加载数据模型]
	 * @param  [type] $classname [类名]
	 * @return [type]            [description]
	 */
	public static function load_model($classname) {
		return self::_load_class($classname,'model');
	}

	/**
	 * [加载应用函数库]
	 * @param  [type] $func [函数库名]
	 * @param  string $m    [模型名]
	 * @return [type]       [description]
	 */
	public static function load_app_func($func, $m = '') {
		$m = empty($m) && defined('ROUTE_M') ? ROUTE_M : $m;
		if (empty($m)) return false;
		return self::_load_func($func, 'modules'.DIRECTORY_SEPARATOR.$m.DIRECTORY_SEPARATOR.'functions');
	}
}











